package com.project.file.tool;

import com.project.bean.file.SaveFileOutDTO;
import com.project.core.util.CommonUtil;
import io.minio.*;
import io.minio.http.Method;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.InputStream;

/**
 * @author mengxj
 */
@Slf4j
@Component
public class MinIoFile extends BaseFile {

    private static MinIoFile instance;

    @Resource
    public void setInstance(MinIoFile instance) {
        MinIoFile.instance = instance;
    }

    public static BaseFile getInstance(){
        return instance;
    }

    @Resource
    private MinioClient minioClient;

    @Override
    public boolean removeFile(String bucket, String objectName) {
        try {
            RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder().bucket(bucket).object(objectName).build();
            this.minioClient.removeObject(removeObjectArgs);
            return true;
        }catch (Exception e){
            String msg = String.format("删除文件失败! 参数：bucket %s objectName %s, 错误信息：==> %s: %s", bucket, objectName, e.getClass(), e.getMessage());
            CommonUtil.throwException(msg, e);
            return false;
        }
    }

    @Override
    public SaveFileOutDTO saveFile(String fileName, String bucket, InputStream is, String suffix) throws Exception {
        log.info("校验桶是否存在，不存在则创建");
        BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder().bucket(bucket).build();
        boolean isExist = this.minioClient.bucketExists(bucketExistsArgs);
        if(!isExist){
            log.info("桶 {} 不存在, 开始创建!", bucket);
            MakeBucketArgs makeBucketArgs = MakeBucketArgs.builder().bucket(bucket).build();
            this.minioClient.makeBucket(makeBucketArgs);
        }
        String objectName = super.getObjectName(suffix, fileName);
        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .contentType("application/octet-stream").bucket(bucket)
                .object(objectName)
                .stream(is, is.available(), -1).build();
        this.minioClient.putObject(putObjectArgs);
        GetPresignedObjectUrlArgs urlArgs = GetPresignedObjectUrlArgs.builder().object(objectName).bucket(bucket).method(Method.GET).build();
        String url = this.minioClient.getPresignedObjectUrl(urlArgs);
        log.info("保存文件完成，地址为：{}", url);
        return SaveFileOutDTO.builder().fileName(fileName).suffix(suffix).bucket(bucket).objectName(objectName).url(url).build();
    }

    @Override
    public void download(String bucket, String objectName, String fileName){
        try {
            DownloadObjectArgs downloadObjectArgs = DownloadObjectArgs.builder().bucket(bucket).object(objectName).filename(fileName).build();
            this.minioClient.downloadObject(downloadObjectArgs);
        }catch (Exception e){
            String msg = String.format("下载文件失败! 参数：bucket %s objectName %s, 错误信息：==> %s: %s", bucket, objectName, e.getClass(), e.getMessage());
            CommonUtil.throwException(msg, e);
        }
    }
}
